<div class="add-subcat">
    <form id="form1" class="layui-form layui-form-pane" action="/education/admin/category/save">
        <div class="layui-form-item">
            <label class="layui-form-label">上级</label>
            <div class="layui-input-inline">
                <input type="hidden" name="parent_id" >
                <input type="text" name="parentname" class="layui-input" readonly>
            </div>
            <a class="layui-btn" onclick="beforeChoice();">
                <i class="iconfont">&#xe649;</i> 选择
            </a>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" name="category_no" required jq-verify="required" jq-error="请输入编号"
                       class="layui-input ">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="text" name="category_name" required jq-verify="required" jq-error="请输入名称"
                       class="layui-input ">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图标</label>
            <div class="layui-input-block">
                <input type="file" name="file" class="layui-upload-file" id="img">
                <input type="hidden" name="category_icon"  error-id="img-error">
                <p id="img-error" class="error"></p>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" name="category_ord" required jq-verify="number" jq-error="排序必须为数字"
                       class="layui-input ">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <select name="category_status">
                    <option value="">请选择</option>
                    <option value="0">停用</option>
                    <option value="1">启用</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" jq-submit jq-filter="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
var treeIndex;
    function beforeChoice(){
        treeIndex = layer.open({
            type: 2,
            content: "base-tree.html?url=/education/admin/category/treeData", "title": "选择", "area": ['500px',
                '600px'], "maxmin":"true"
        });
    }

    function afterTreeSelected(item) {
        var $ = layui.jquery;
        $('input[name="parent_id"]').val(item.id);
        $('input[name="parentname"]').val(item.name);
        layer.close(treeIndex);
    }

    layui.use('upload', function (ex) {
        var box;
        //上传文件设置
        layui.upload({
            elem:'#img',
            url: '/education/api/image/category',
            before: function(input) {
                box = $(input).parent('form').parent('div').parent('.layui-input-block');
                if (box.next('div').length > 0) {
                    box.next('div').html('<div class="imgbox"><p>上传中...</p></div>');
                } else {
                    box.after('<div class="layui-input-block"><div class="imgbox"><p>上传中...</p></div></div>');
                }
            },
            success: function(res) {console.log(res)
                if (res.code == 200) {
                    box.next('div').find('div.imgbox').html('<img src="/education' + res.data[0].url + '" alt="..." class="img-thumbnail">');
                    box.find('input[type=hidden]').val(res.data[0].url);
                    form.check(box.find('input[type=hidden]'));
                } else {
                    box.next('div').find('p').html('上传失败...')
                }
            }
        });

    });

</script>